package com.example.InterFace2;


import com.sun.org.apache.xerces.internal.impl.xpath.XPath;

import java.awt.image.DirectColorModel;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ConnectMysql {

    private Connection con ;
    private PreparedStatement ps;
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    //数据库连接地址
    private static final String URL = "jdbc:mysql://042.192.208.23/bike2.0?characterEncoding=utf8&useSSL=true";
    private static final String USER_NAME="bike2.0" ; //用户名
    private static final String PASSWORD="bike123456" ; //密码

    ConnectMysql(){
        try {
            Class.forName(DRIVER_NAME);
            this.con = DriverManager.getConnection(URL,USER_NAME,PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 将输入的字符串进行模糊处理，返回用于查询的模糊语句
     * @param s 输入的字符串
     * @return  返回模糊处理后的字符串
     */
    public static String Fuzzy(String s){

        StringBuilder sb = new StringBuilder("%"+s);
        for(int i=2;i<sb.length();i++){
            sb.insert(i,"%");
            i++;
        }
        sb.append("%");
        return sb.toString();
    }
    public ResultSet selectBike(String bikeName){
        try {

            String sql = "select * from product where brand like ? or p_modle like ? or product.p_name like ?";
            ps = con.prepareStatement(sql);
            ps.setString(1,Fuzzy(bikeName));
            ps.setString(2,Fuzzy(bikeName));
            ps.setString(3,Fuzzy(bikeName));
            System.out.println(sql);
            return  ps.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    public String  giveImageAdr(String id){
        try {
            String sql = "select  pic_adr from color where p_num=?";
            ps = con.prepareStatement(sql);
            ps.setString(1,id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                return rs.getString("pic_adr");
            }else {
                return "暂无数据";
            }

        }catch (Exception e){
            e.printStackTrace();
            return "数据错误";
        }
    }


    public void addHot(String brand){

        try {
            String sql ="update hot_degrees set hot_degrees=hot_degrees+1 where brand=?";
            ps = con.prepareStatement(sql);
            ps.setString(1,brand);
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();

        }
    }

    public List<String> giveHot(){
        try {
            String sql = "select* from hot_degrees order by hot_degrees desc";
            ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            List<String> list = new ArrayList<>();
            int count=0;
            while (rs.next()){
                count++;
                list.add(rs.getString("brand"));
                if(count==4) break;
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public void close(){
        try {
            ps.close();
            con.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void main(String[] arg){

        ConnectMysql connectMysql = new ConnectMysql();
        try {
//
          List<String>list =  connectMysql.giveHot();
          for (int i=0;i<4;i++){
              System.out.println(list.get(i));
          }
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println(Fuzzy("bike8"));
    }
}
